package com.shbaiche.bifenba.ui.my;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.google.gson.JsonObject;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.GirlAdapter;
import com.shbaiche.bifenba.adapter.my.MessageListAdapter;
import com.shbaiche.bifenba.model.Event;
import com.shbaiche.bifenba.model.GankResults;
import com.shbaiche.bifenba.model.PushModel;
import com.shbaiche.bifenba.ui.base.BaseActivity;
import com.shbaiche.bifenba.ui.base.BasePagerActivity;
import com.shbaiche.bifenba.ui.base.WebActivity;
import com.shbaiche.bifenba.util.SystemUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.base.XActivity;
import cn.droidlover.xdroid.event.BusFactory;
import cn.droidlover.xdroidbase.router.Router;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerView;


/**
 * Created by wanglei on 2016/12/9.
 */

public class MessageListActivity extends BasePagerActivity {

    MessageListAdapter adapter;


    @Override
    public String setTitle() {
        return "我的消息";
    }


    @Override
    public SimpleRecAdapter getAdapter() {

        isNoReFresh = true;

        if (adapter == null) {

            List<PushModel>list = new ArrayList<>();
            try {
                String pushStr = SystemUtil.loadPreference("push");
                if (!TextUtils.isEmpty(pushStr) && pushStr.length() > 4) {
                    String[] arry = pushStr.split("\\^");
                    PushModel mo = null;
                    for (int i = 0; i < arry.length; i++) {
                        String json = arry[i];
                        JSONObject obj = new JSONObject(json);
                        String title = obj.getString("t");
                        String content = obj.getString("c");
                        mo = new PushModel();
                        mo.setTitle(title);
                        mo.setContent(content);
                        try {
                            mo.setTime(obj.getString("time"));
                        }catch (Exception e){

                        }
                        list.add(mo);
                    }
                }
            }catch (Exception e){

            }

            adapter = new MessageListAdapter(context);
            adapter.setRecItemClick(new RecyclerItemCallback<PushModel, MessageListAdapter.ViewHolder>() {
                @Override
                public void onItemClick(int position, PushModel model, int tag, MessageListAdapter.ViewHolder holder) {
                    super.onItemClick(position, model, tag, holder);
                }
            });
            adapter.setData(list);
        }
        return adapter;
    }

    @Override
    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(this);
    }

    @Override
    public String getType() {
        return "福利";
    }


    public static void launch(Activity activity) {
        Router.newIntent(activity)
                .to(MessageListActivity.class)
                .data(new Bundle())
                .launch();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {

        super.onResume();
    }
}
